//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2005
//
// Device-dependent bitmap.
//

# include "StdAfx.h"
# include "DDBitmap.h"
# include "Wrappers.h"
# include "Globals.h"

CDDBitmap::CDDBitmap (long _lWidth, long _lHeight) :
	CBitmap (_lWidth, _lHeight)
{
	CClientDC hdcScreen (NULL);

	m_hMemDC = ::CreateCompatibleDC (hdcScreen);
	m_hBitmap = ::CreateCompatibleBitmap (hdcScreen, _lWidth, _lHeight);
	::SelectObject (m_hMemDC, m_hBitmap);
}

CDDBitmap::~CDDBitmap () {
	if (m_hMemDC) {
		::DeleteDC (m_hMemDC);
		m_hMemDC = NULL;
	}
	if (m_hBitmap) {
		::DeleteObject (m_hBitmap);
		m_hBitmap = NULL;
	}
}

BOOL CDDBitmap::AlphaBlend (
	HDC _hdcDest,
	int _xDest, int _yDest, int _wDest, int _hDest,
	int _xSrc,  int _ySrc,  int _wSrc,  int _hSrc,
	BYTE _SourceConstantAlpha
) const {
	if (g_fpAlphaBlend) {
		::SetStretchBltMode (_hdcDest, COLORONCOLOR);

		const BLENDFUNCTION bf = {AC_SRC_OVER, 0, _SourceConstantAlpha, 0};
		return g_fpAlphaBlend (
			_hdcDest, _xDest, _yDest, _wDest, _hDest,
			m_hMemDC, _xSrc,  _ySrc,  _wSrc,  _hSrc,
			bf
		);
	} else
		return FALSE;
}

CBitmap * CDDBitmap::MakeSmall () const {
	CDDBitmap * const pBitmap = new CDDBitmap (Width ()/2, Height ()/2);
	::SetStretchBltMode (pBitmap->GetDC (), HALFTONE);
	::StretchBlt (pBitmap->GetDC (), 0, 0, pBitmap->Width (), pBitmap->Height (), m_hMemDC, 0, 0, m_lWidth, m_lHeight, SRCCOPY);
	return pBitmap;
}

